package com.next

import grails.converters.JSON
import grails.gorm.transactions.Transactional
import groovy.json.JsonOutput
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

@Transactional
@CompileStatic
@TypeChecked
class ContactService
{
    static scope = "singleton"

    def setRequest(String code, String state)
    {
        def response
        def encode = "UTF-8"
        def CODE = code

        // DefaultDefault
        def APPID = "wx759dbfa790cfd63b"
        def SECRET = "0a03f60f673802a1b32e577590c78347"

        URL url = new URL("https://api.weixin.qq" + ".com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type" + "=authorization_code")
        def params = "APPID=" + URLEncoder.encode(APPID, encode) + "&SECRET=" + URLEncoder.encode(SECRET, encode) + "&CODE=" + URLEncoder.encode(CODE, encode)

        HttpURLConnection connection = (HttpURLConnection) url.openConnection()
        connection.setDoOutput(true)
        connection.setRequestMethod("POST")
        connection.outputStream.withWriter { Writer writer -> writer.write params }
        response = connection.inputStream.withReader { Reader reader -> reader.text }
        def openId = JSON.parse(response).getAt("openid")
        println "微信公众平台响应：" + response
        println "微信公众平台返回OpenID：" + openId
        return openId
    }

    def toJson(Contact contact)
    {
        def fullName = ""
        def city = ""
        def avatar = ""
        def userCode = ""
        def appSessionId = ""
        def level = ""
        def cellphone = ""
        def userName = ""
        def idNumber = ""
        def bankName = ""
        def bankAccount = ""

        if (contact?.fullName)
        {
            fullName = contact.fullName
        }
        if (contact?.city?.name)
        {
            city = contact.city?.name
        }
        if (contact?.avatar)
        {
            avatar = contact.avatar
        }
        if (contact?.userCode)
        {
            userCode = contact.userCode
        }
        if (contact?.appSessionId)
        {
            appSessionId = contact.appSessionId
        }
        if (contact?.level)
        {
            level = contact.level?.name
        }
        if (contact?.cellphone)
        {
            cellphone = contact.cellphone
        }
        if (contact?.user?.fullName)
        {
            userName = contact.user?.fullName
        }
        if (contact?.idNumber)
        {
            idNumber = contact.idNumber
        }
        if (contact?.bankName)
        {
            bankName = contact.bankName
        }
        if (contact?.bankAccount)
        {
            bankAccount = contact.bankAccount
        }

        def data = [fullName   : fullName, city: city, avatar: avatar, userCode: userCode, appSessionId: appSessionId, level: level, cellphone: cellphone,
                    userName   : userName, idNumber: idNumber, bankName: bankName,
                    bankAccount: bankAccount]
        println "data: " + data
        return JsonOutput.toJson(data)
    }

    Contact findContactBySessionId(String sessionId)
    {
    }

    /**
     * @Author 班旭娟
     * @ModifiedDate 2017-4-21
     */
    Boolean verifyIdNumber(String idNumber)
    {
        def city = [11: "北京",
                    12: "天津",
                    13: "河北",
                    14: "山西",
                    15: "内蒙古",
                    21: "辽宁",
                    22: "吉林",
                    23: "黑龙江 ",
                    31: "上海",
                    32: "江苏",
                    33: "浙江",
                    34: "安徽",
                    35: "福建",
                    36: "江西",
                    37: "山东",
                    41: "河南",
                    42: "湖北 ",
                    43: "湖南",
                    44: "广东",
                    45: "广西",
                    46: "海南",
                    50: "重庆",
                    51: "四川",
                    52: "贵州",
                    53: "云南",
                    54: "西藏 ",
                    61: "陕西",
                    62: "甘肃",
                    63: "青海",
                    64: "宁夏",
                    65: "新疆",
                    71: "台湾",
                    81: "香港",
                    82: "澳门",
                    91: "国外 "]
        if (!(idNumber.matches(/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/)))
        {
            return false
        } else if (!city[Integer.parseInt(idNumber.substring(0, 2))])
        {
            return false
        } else
        {
            if (idNumber.length() == 18)
            {
                def factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                def parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
                def sum = 0;
                def ai = 0;
                def wi = 0;
                for (
                    int i = 0;
                    i < 17;
                    i++)
                {
                    ai = Integer.parseInt(String.valueOf(idNumber.charAt(i)));
                    wi = factor[i];
                    sum += ai * wi;
                }
                def index = sum % 11;

                if (index == 2 && idNumber[17] != 'X') //&& idNumber[17] != 'x'
                {
                    return false;
                }
                if (index != 2 && !parity[sum % 11].toString().equals(String.valueOf(idNumber.charAt(17))))
                {
                    return false;
                }
            }
        }
        return true
    }

    /**
     * @Author 班旭娟
     * @ModifiedDate 2017-4-21
     */
    def indexByAgent(User user, Integer offset)
    {
        //Integer offset
        Integer max = 10

        def list = []
        Role role = Role.find("from Role where authority = 'ROLE_ACCOUNT_MANAGER'")
        UserRole userRole = UserRole.find("from UserRole as ur where ur.user.id = ${user.id} and ur.role.id = ${role.id}")
        if (userRole)
        {
            list = Contact.findAll("from Contact as c where c.user.id = ${user.id} and c.type = 'Agent' order by " + "createdDate desc", [max: max, offset: offset])
        }
        role = Role.find("from Role where authority = 'ROLE_BRANCH_OFFICE_POST_LOAN_MANAGER'")
        userRole = UserRole.find("from UserRole as ur where ur.user.id = ${user.id} and ur.role.id = ${role.id}")
        if (userRole)
        {
            list = Contact.findAll("from Contact as c where c.user.city.id = ${user?.city?.id} and c.type = 'Agent' order by c.createdDate desc", [max   : max,
                                                                                                                                                   offset: offset])
        }
        role = Role.find("from Role where authority = 'ROLE_BRANCH_GENERAL_MANAGER'")
        userRole = UserRole.find("from UserRole as ur where ur.user.id = ${user.id} and ur.role.id = ${role.id}")
        if (userRole)
        {
            list = Contact.findAll("from Contact as c where c.user.city.id = ${user?.city?.id} and c.type = 'Agent' order by c.createdDate desc", [max   : max,
                                                                                                                                                   offset: offset])
        }
        role = Role.find("from Role where authority = 'ROLE_ADMINISTRATOR'")
        userRole = UserRole.find("from UserRole as ur where ur.user.id = ${user.id} and ur.role.id = ${role.id}")
        if (userRole)
        {
            list = Contact.findAll("from Contact as c where c.type = 'Agent' order by c.createdDate desc", [max   : max,
                                                                                                            offset: offset])
        }

        return list
    }

    /**
     * @Author 班旭娟
     * @ModifiedDate 2017-4-21
     */
    def indexByClient(User user, Integer offset)
    {

        Integer max = 10

        def list = []
        Role role = Role.find("from Role where authority ='ROLE_ACCOUNT_MANAGER'")
        UserRole userRole = UserRole.find("from UserRole  as ur where ur.user.id=${user.id} and ur.role.id = ${role.id}")
        if (userRole)
        {
            list = Contact.findAll("from Contact as c where c.user.id=${user.id} and (c.type='Client' or c.type is null) order by " + "createdDate desc", [max: max, offset: offset])
        }
        role = Role.find("from Role where authority ='ROLE_BRANCH_OFFICE_POST_LOAN_MANAGER'")

        userRole = UserRole.find("from UserRole as ur where ur.user.id = ${user.id} and ur.role.id = ${role.id}")

        if (userRole)
        {
            list = Contact.findAll("from Contact as c where c.user.city.id = ${user?.city?.id} and c.type='Client' or c.type is null order by createdDate desc", [max   : max,
                                                                                                                                                                  offset: offset])
        }
        role = Role.find("from Role where authority ='ROLE_BRANCH_GENERAL_MANAGER'")

        userRole = UserRole.find("from UserRole as ur where ur.user.id = ${user.id} and ur.role.id = ${role.id}")

        if (userRole)
        {
            list = Contact.findAll("from Contact as c where c.user.city.id = ${user?.city?.id} and c.type='Client' or c.type is null order by createdDate desc", [max   : max,
                                                                                                                                                                  offset: offset])
        }

        role = Role.find("from Role where authority ='ROLE_ADMINISTRATOR'")

        userRole = UserRole.find("from UserRole as ur where ur.user.id = ${user.id} and ur.role.id = ${role.id}")

        if (userRole)
        {
            list = Contact.findAll("from Contact as c where  c.type='Client' or c.type is null order by createdDate desc", [max   : max,
                                                                                                                            offset: offset])
        }


        return list

    }

    def generateVerifiedCode()
    {
        def verifiedCode = ""
        for (
            int i = 0;
            i < 6;
            i++)
        {
            verifiedCode += (int) Math.floor(Math.random() * 10)
        }

        return verifiedCode
    }

}
